//
//  NNClipViewController.swift
//  CommonTool
//
//  Created by Name i on 2022/3/17.
//

import Foundation
import UIKit


class NNClipViewController: UIViewController {
    
    
    private var clipView1: NNImageClipView!
    private var clipView2: NNImageClipView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.backgroundColor = .white
        
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "裁剪", style: .done,
                                                            target: self,
                                                            action: #selector(clipImage))
        
        
        
        guard let image = UIImage(named: "bg2") else { return }
        let imageSize = NNImageClipView.contentSize(contentWidth: view.frame.size.width, image: image)
        
        clipView1 = NNImageClipView(frame: CGRect(x: 0,
                                                      y: 100,
                                                      width: view.frame.size.width,
                                                      height: imageSize.height))
        
        
        clipView1.config(image: image, clipScale: 120.0 / 88.5) { selectedRect in
            CC.log("clipView1 选择的区域为： \(selectedRect)")
        }
        
        clipView2 = NNImageClipView(frame: CGRect(x: 0,
                                                      y: clipView1.frame.maxY + 20,
                                                      width: view.frame.size.width,
                                                      height: imageSize.height))
        clipView2.config(image: image, clipScale: 156.5 / 88.5) { selectedRect in
            CC.log("clipView2 选择的区域为： \(selectedRect)")
        }

        view.addSubviews(childs: [clipView1, clipView2])
    }
    
    
    @objc func clipImage() {
        guard let img1 = clipView1.clipImage(), let img2 = clipView2.clipImage() else { return }
        let imageEditController = NNImageEditController(originImage1: img1, originImage2: img2)
        self.navigationController?.pushViewController(imageEditController, animated: true)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        CC.log("viewWillAppear")
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        CC.log("viewWillDisappear")
    }
}
